﻿Namespace EIB.Bus.FT12
    Public MustInherit Class FT12_FrameControl
        Inherits FT12_FrameBase

        Protected _Control As Byte = Nothing
        Protected _Direction As FT12_FrameDirection = Nothing
        Protected _PrimMessage As Boolean = Nothing
        'primary message
        Protected _ToggleFCB As Boolean = Nothing
        Protected _FCV As Boolean = Nothing
        'secondary message
        Protected _dataFlowControl As Boolean = Nothing
        Protected _messageCode As Byte = Nothing


        Sub New(type As FT12_FrameType, direction As FT12_FrameDirection, toggleFCB As Boolean, FCV As Boolean, messageCode As FT12_FramePrimaryMessageCode)
            MyBase.New(type)

            If Not [Enum].IsDefined(GetType(FT12_FrameDirection), direction) Then Throw New ArgumentException("")
            If Not [Enum].IsDefined(GetType(FT12_FramePrimaryMessageCode), messageCode) Then Throw New ArgumentException("")


            _Direction = direction
            _PrimMessage = True
            _ToggleFCB = toggleFCB
            _FCV = FCV
            _messageCode = messageCode

            _Control = (_Direction << 7) + (1 << 6) + (If(_ToggleFCB, 1, 0) << 5) + (If(_FCV, 1, 0) << 4) + (_messageCode And &HF)
        End Sub

        Sub New(type As FT12_FrameType, direction As FT12_FrameDirection, dataFlowControl As Boolean, messageCode As FT12_FrameSecondaryMessageCode)
            MyBase.New(type)

            If Not [Enum].IsDefined(GetType(FT12_FrameDirection), direction) Then Throw New ArgumentException("")
            If Not [Enum].IsDefined(GetType(FT12_FrameSecondaryMessageCode), messageCode) Then Throw New ArgumentException("")

            _Direction = direction
            _PrimMessage = False
            'Reserved
            _dataFlowControl = dataFlowControl

            _Control = (_Direction << 7) + (If(_dataFlowControl, 1, 0) << 4) + (_messageCode And &HF)
        End Sub

        Sub New(busData As Byte(), controlIndex As Byte)
            MyBase.New(busData)

            _Control = busData(controlIndex)
            _Direction = (_Control And &H80) >> 7
            _PrimMessage = (_Control And &H40) >> 6
            If _PrimMessage Then
                _ToggleFCB = (_Control And &H20) >> 5
                _FCV = (_Control And &H10) >> 4
            Else
                'reserved
                _dataFlowControl = (_Control And &H10) >> 4
            End If
            _messageCode = _Control And &HF
        End Sub


        Public ReadOnly Property Control As Byte
            Get
                Return _Control
            End Get
        End Property

        Public ReadOnly Property Direction As FT12_FrameDirection
            Get
                Return _Direction
            End Get
        End Property

        Public ReadOnly Property PrimaryMessage As Boolean
            Get
                Return _PrimMessage
            End Get
        End Property

        Public ReadOnly Property ToggleFCB As Boolean?
            Get
                Return _ToggleFCB
            End Get
        End Property

        Public ReadOnly Property FCV As Boolean?
            Get
                Return _FCV
            End Get
        End Property

        Public ReadOnly Property DataFlowControl As Boolean?
            Get
                Return _dataFlowControl
            End Get
        End Property

        Public ReadOnly Property MessageCode As [Enum]
            Get
                Return If(_PrimMessage, CType(_messageCode, FT12_FramePrimaryMessageCode), CType(_messageCode, FT12_FrameSecondaryMessageCode))
            End Get
        End Property
    End Class
End Namespace